<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Crypt;
use Mail;


class EnterpriseController extends Controller
{
    public function indexe($id)
    {
        $wsale = DB::table('recruit')
                    -> where('e_id',$id)
                    -> where('sale',0)
                    -> count();
        // 已支付的职位订单sale = 1
        $ysale = DB::table('recruit')
                    -> where('e_id',$id)
                    -> where('sale',1)
                    -> count();
        // 发布中的职位 sale = 1 and status = 1
        $ystatus = DB::table('recruit')
                    -> where('e_id',$id)
                    -> where('sale',1)
                    -> where('status',1)
                    -> count();
        // 待审核的职位 sale = 1 and status = 0
         $wstatus = DB::table('recruit')
                    -> where('e_id',$id)
                    -> where('sale',1)
                    -> where('status',0)
                    -> count();
        // 职位余额sales
        $sales = DB::table('recruit')
                    -> select('sales')
                    -> where('e_id',$id)
                    -> first() -> sales;
        // dd($sales);
        // dd(session());
        return view('home.enterprises.index',['id' => $id,'wsale' => $wsale,'ysale' => $ysale,'ystatus' => $ystatus,'wstatus' => $wstatus,'sales' => $sales]);

    }
    // 企业会员用户中心 index
    public function index(Request $req)
    {
    	$data = $req -> except('_token','Submit');
        // dd($data);
        if($req -> CheckCode != session('code'))
        {
            $req -> flashExcept('_token','Submit');
            return back();
        }
        // dd(Crypt::encrypt($data['Password']));
        // dd(session('code'));
        // 用户登录session()
    	$userdata = DB::table('enterprises') -> select() -> where('email',$req -> LoginName) -> first();
    	if(empty($userdata))
        {
            $req -> flashExcept('_token','Submit');
            return back() -> with(['info' => '邮箱不存在']);
        }
        $pass = Crypt::decrypt($userdata -> passwd);
        if($req -> Password != $pass)
        {
            
                $req -> flashExcept('_token','Submit');
                return back() -> with(['info' => '密码错误']);
            
        }
        session(['masterarr.user' => $userdata -> id]);
        // dd($pass);
    	session(['master['.$userdata -> id.']' => $userdata]);
    	// $data = DB::table('enterprises') -> select() -> where('id',10102) -> first();
    	// session(['master['.$data -> id.']' => $data]);
        // 未支付的职位订单sale = 0
        $wsale = DB::table('recruit')
                    -> where('e_id',$userdata -> id)
                    -> where('sale',0)
                    -> count();
        // 已支付的职位订单sale = 1
        $ysale = DB::table('recruit')
                    -> where('e_id',$userdata -> id)
                    -> where('sale',1)
                    -> count();
        // 发布中的职位 sale = 1 and status = 1
        $ystatus = DB::table('recruit')
                    -> where('e_id',$userdata -> id)
                    -> where('sale',1)
                    -> where('status',1)
                    -> count();
        // 待审核的职位 sale = 1 and status = 0
         $wstatus = DB::table('recruit')
                    -> where('e_id',$userdata -> id)
                    -> where('sale',1)
                    -> where('status',0)
                    -> count();
        // 职位余额sales
    	$sales = DB::table('recruit')
                    -> select('sales')
                    -> where('e_id',$userdata -> id)
                    -> first() -> sales;
        // dd($sales);
        // dd(session());
    	return view('home.enterprises.index',['id' => $userdata -> id,'wsale' => $wsale,'ysale' => $ysale,'ystatus' => $ystatus,'wstatus' => $wstatus,'sales' => $sales]);
    }
    public function myjobs($id,Request $r)
    {
        if($r -> token != session('master['.$id.']') -> token)
        {
            return view('home.enterprises.error');
        }
        // dd();
        // dd($r);
        $cate = DB::table('cate') -> where('pid','<>','0') ->  get();
        $er = DB::table('er') -> orderBy('id') ->  get();
        $we = DB::table('we') -> orderBy('id') ->  get();
        $pms = DB::table('pms') ->  get();
        $label = DB::table('label') ->  get();

        // dd($er);

        // 只要二级三级分类
        $cateData = [];
        $cateSDate = [];
        foreach ($cate as $key => $value) {
           $num = substr_count($value -> path,',');
           // dd($num);
           if($num == 1)
           {
                // 等于一个是二级分类
                $cateData[]=$value;
           }else if($num == 2)
           {
                $cateSDate[$value -> pid][]=$value;
           }
            // substr_c
        }
        // dd($cateData);
        $industrycateData = DB::table('industrycate') -> get();
        $enterpriseData = DB::table('enterprises AS e')
            -> leftJoin('regioncate AS r','e.city','=','r.id')
            -> leftJoin('regioncate AS re','e.province','=','re.id')
            -> leftJoin('industrycate AS i','e.firstIndustry','=','i.id')
            -> leftJoin('corptype AS ct','e.corptype','=','ct.id')
            -> leftJoin('corpsize AS cs','e.corpsize','=','cs.id')
            -> select('e.*','r.name AS cityN','re.name AS provinceN','i.name AS firstIndustryN','ct.name AS ctN','cs.name AS csN')
            -> where('e.id',$id) -> first();
         if(empty($enterpriseData))
         {
            return view('errors.503');
         }
         if($enterpriseData -> industrylist)
        {
            $tmparr = explode(',',$enterpriseData -> industrylist);
            $enterpriseData -> industrylist = array();
            foreach($tmparr as $k => $v)
            {
                $enterpriseData -> industrylist[DB::table('industrycate') -> where('id',$v) -> select('name') -> first() -> name] = $v;
            }
        }
        if($enterpriseData -> tel)
        {
            $enterpriseData -> tel = explode('-',$enterpriseData -> tel); 
        }
        // array_shift($enterpriseData -> industrylist);
        session(['master['.$id.']' => $enterpriseData]);
        // dd($enterpriseData);
        // dd($industrycateData);
        $pidData = DB::table('regioncate') -> where('pid',0) -> where('status',0) -> get();
        $mainData = DB::table('regioncate') -> where('pid',0) -> where('status',1) -> get();
        $idData = [];
        foreach($pidData as $k => $v)
        {
            $idData[] = DB::table('regioncate') -> where('pid',$v -> id) -> get();
        }
        // dd($mainData);
        
        
        return view('home.enterprises.myjobs',['id' => $id,'pidData' => $pidData,'idData' => $idData,'mainData' => $mainData,'industryData' => $industrycateData,'cateData' => $cateData,'cateSData' => $cateSDate,'pms' => $pms,'er' => $er,'we' => $we,'label' => $label]);
        // return view('home.enterprises.myjobs',['id' => $id]);
    }
    public function modify($id)
    {
        $industrycateData = DB::table('industrycate') -> get();
        $enterpriseData = DB::table('enterprises AS e')
            -> leftJoin('regioncate AS r','e.city','=','r.id')
            -> leftJoin('regioncate AS re','e.province','=','re.id')
            -> leftJoin('industrycate AS i','e.firstIndustry','=','i.id')
            -> leftJoin('corptype AS ct','e.corptype','=','ct.id')
            -> leftJoin('corpsize AS cs','e.corpsize','=','cs.id')
            -> select('e.*','r.name AS cityN','re.name AS provinceN','i.name AS firstIndustryN','ct.name AS ctN','cs.name AS csN')
            -> where('e.id',$id) -> first();
       // dd($enterpriseData);
        if($enterpriseData -> industrylist)
        {
            $tmparr = explode(',',$enterpriseData -> industrylist);
            $enterpriseData -> industrylist = array();
            foreach($tmparr as $k => $v)
            {
                $enterpriseData -> industrylist[DB::table('industrycate') -> where('id',$v) -> select('name') -> first() -> name] = $v;
            }
        }
        if($enterpriseData -> tel)
        {
            $enterpriseData -> tel = explode('-',$enterpriseData -> tel); 
        }
        
        
        // array_shift($enterpriseData -> industrylist);
        session(['master['.$id.']' => $enterpriseData]);
        // dd($enterpriseData);
        // dd($industrycateData);
        $pidData = DB::table('regioncate') -> where('pid',0) -> where('status',0) -> get();
        $mainData = DB::table('regioncate') -> where('pid',0) -> where('status',1) -> get();
        $idData = [];
        foreach($pidData as $k => $v)
        {
            $idData[] = DB::table('regioncate') -> where('pid',$v -> id) -> get();
        }
        // dd($mainData);
        
        
        return view('home.enterprises.modify',['id' => $id,'pidData' => $pidData,'idData' => $idData,'mainData' => $mainData,'industryData' => $industrycateData]);
    }
    public function changes($id,Request $r)
    {
        
        // dd($id);
        // dd($r -> all());

        // sleep(5);
        $data = $r -> except('_token','textInput_comp_contact0','textInput_comp_contact1','textInput_comp_contact2','textInput_comp_contact5');
        $data1 = [];
        // 公司名称
        $data1['compname']= $data['corpname'];
        // 所在省直辖市
        $data1['province'] = $data['add2'];
        /// // 公司所在地市
        $data1['city']=trim(strrchr($data['citywork'],'_'),'_');
        // $data1[''];
         // 公司联系人
        $data1['contactor'] = $data['payuser'];
        // 公司固话
        if(!empty($data['tel_1']) && !empty($data['tel_1']))
        {
            $data1['tel'] = $data['tel_1'].'-'.$data['tel_2'].'-'.$data['tel_3'];
        }
        // 公司联系人手机
        $data1['phone'] = $data['userMobile'];
        // 公司所属行业
        if($data['position'] == $data['firstIndustry'])
        {
           $data1['firstIndustry'] = $data['firstIndustry'];
        }
        // 公司次要行业
        $data1['industrylist'] = implode(',',$data['industrylist']);
        // 公司性质
        $data1['corptype']=$data['corptype'];
        // 公司规模
        $data1['corpsize']=$data['corpsize'];
        // 公司详细地址
        $data1['address'] = $data['corpAddrDetail'];
        // 营业执照发送方式
        if($data['busiLicense'] == '传真或电邮')
        {
            $data1['busiLicense']=1;
            // $data1['licenseImage']='';
        }else
        {
            $dataid = DB::table('enterprises') -> where('id',$id) -> first();
            // dd($dataid);
            if(!empty($dataid -> licenseImage) && file_exists('./home/uploads/'.$dataid -> licenseImage))
            {
                unlink('./home/uploads/'.$dataid -> licenseImage);
                // unlink(filename);
                
            }
            // die;
            // dd($dataid);
            $data1['busiLicense']=0;
            $data1['authentication']=1;
            // dd($r -> file('licenseImage'));
            // 确认文件是否有上传
            if($r -> hasFile('licenseImage')) {
                // 确认上传的文件是否有效
                if($r -> file('licenseImage')[0] -> isValid()) {
                    // 移动上传的文件
                    $destinationPath = './home/uploads/';
                    $ext = $r -> file('licenseImage')[0] -> getClientOriginalExtension();
                    $basename = Crypt::encrypt($id);
                    $fileName = $basename.'.'.$ext;
                    $data1['licenseImage']=$fileName;
                    $res = $r -> file('licenseImage')[0] -> move($destinationPath, $fileName);
                    if(!$res)
                    {
                        return back() -> with(['info' => '营业执照处理失败']);
                    }
                }
            }
        }
        // 公司介绍 companydetail
        $data1['companydetail'] = $data['companydetail'];
        // 公司联系方式 lxfs
        $data1['lxfs'] = $data['lxfs'];
        $res = DB::table('enterprises') -> where('id',$id) -> update($data1);

        // dd($data1);
        if($res)
        {
            return redirect('/home/enterprise/modify/'.$id) -> with(['info' => '更新成功']);
            return '公司信息更新成功';
        }else
        {
            return redirect('/home/enterprise/modify/'.$id) -> with(['info' => '更新失败']);
            return '更新失败';
        }
        
        
    }
    public function img($r)
    {
        // dd(url('/home/uploads/').$r);
        // dd($r);
        // if(file_exists(url('/home/uploads/').$r))
        // {
          // var_dump(file_exists(url('./home/uploads/').$r));
          // var_dump();
          return  "<img src = '/home/uploads/".$r."'>";
        // }else
        // {
        //   dd('1123');
        // }

    }
    public function jobs(Request $r)
    {
        // dd($r -> all());
        
            $data=$r -> only('id','workname','c_id','detail','label','citywork','MinYears','education','employment_type','jobnum','salary','invoice','itype','icorpname','idesc','rname','rphone','padd2','padd3','raddress','rpostcode');
            // dd($data);
            $data1['workname'] = $data['workname'];
            $data1['c_id'] = $data['c_id'][0];
            $data1['p_id'] = $data['c_id'][1];
            $data1['e_id'] = $data['id'];
            $data1['r_id']=trim(strrchr($data['citywork'],'_'),'_');
            $data1['description'] = $data['detail'];
            $data1['w_id'] = $data['MinYears'];
            $data1['er_id'] = $data['education'];
            $data1['jobcategory'] = $data['employment_type'];
            $data1['num'] = $data['jobnum'];
            $data1['pms_id'] = $data['salary'];
            // 不需要发票
            $data1['invoice_id']=0;
            $data1['status']=0;
            
            $data1['date']=time();
            // dd($data1);
            $id = DB::table('recruit') -> insertGetId($data1);
            // 判断是否需要发票
            if($data['invoice']<>0)
            {
                // 需要
                $indata['invoiceheader'] = $data['itype']; // 发票抬头
                $indata['compname'] = $data['icorpname']; // 公司名称
                $indata['idesc'] = $data['idesc']; // 发票内容
                $indata['rname'] = $data['rname']; // 联系人姓名
                $indata['rphone'] = $data['rphone']; // 联系人手机
                
                if(!empty($data['padd3']))
                {
                    $indata['city_id'] = $data['padd3']; // 发往城市的地区id
                }else
                {
                    $indata['city_id'] = $data['padd2']; // 发往城市的地区id
                }
                $indata['raddress'] = $data['raddress']; // 详细地址
                $indata['rpostcode'] = $data['rpostcode']; // 邮编
                // 招聘职位id
                $indata['rec_id'] = $id;
                // 企业id
                $indata['e_id'] = $data['id'];
                $inId = DB::table('invoice') -> insertGetId($indata);
                if($inId)
                {
                    $res = DB::table('recruit') -> where('id',$id) -> update(['invoice_id' => $inId]);
                    if(!$res)
                    {
                        return back() -> with('职位发布失败');
                    }
                }
            }
            // 标签表中插入数据
            $inte = [];
            $resin=1;
            if(!empty($data['label']))
            {
               foreach ($data['label'] as $key => $value) {
                $inte[$key]['rec_id'] = $id;
                $inte[$key]['l_id'] = $value;
                }
                $resin = DB::table('recruit_label') -> insert($inte); 
            }
            
            if($resin)
            {
                if(session('master['.$r -> id.']') -> authentication == '5')
                {
                    return redirect('/home/enterprise/jobs/pay/'.$r -> id.'/'.$id.'?token='.session('master['.$r -> id.']') -> token);
                }else
                {
                   $this -> changes($r -> id,$r);
                   return redirect('/home/enterprise/jobs/index/'.$r -> id.'?token='.session('master['.$r -> id.']') -> token) -> with(['info' => '抱歉你未通过审核,所发布的职位不能支付']);
                }
                // dd('职位发布成功');
                return redirect('/home/enterprise/jobs/index/'.$r -> id.'?token='.session('master['.$r -> id.']') -> token) -> with(['info' => '职位发布成功']);
                
            }else
            {
                dd('职位失败');
            }
        
        
        // $this -> changes($r -> id,$r);
    }
    // 我的职位方法
    public function jobsindex($id,Request $r)
    {
        if(empty(session('master['.$id.']')) || $r -> token != session('master['.$id.']') -> token)
        {
            return view('home.enterprises.error');
        }
        $data = DB::table('recruit as rec') 
                -> leftJoin('regioncate as reg','rec.r_id','=','reg.id')
                -> select('rec.*','reg.name')
                -> where('e_id',$id)
                -> paginate(10);
        if(!count($data))
        {
           return  $this -> myjobs($id,$r);
        }
        // dd($data);
        $resume=[];
        foreach($data as $k => $v)
        {
            $resume[] = DB::table('resume_recruit as resc')
                            -> where('rec_id',$v -> id)
                            -> count();
        }
        // dd($resume);
        // dd($r);
        // dd($data);
        // $count = count($data);
        // dd($count);

        // dd(session('master['.$id.']'));
        // 用户登录session()
        // $data = DB::table('enterprises') -> select() -> where('id',1) -> first();
        // dd($id);
        // session(['master['.$data -> id.']' => $data]);
        // $data = DB::table('enterprises') -> select() -> where('id',2) -> first();
        // session(['master['.$data -> id.']' => $data]);

        // dd(session());
        return view('home.enterprises.jobindex',['id' => $id,'resume' => $resume,'data' => $data,'request' => $r -> all()]);
    }
    // 我的职位预览
    public function jobsview($eid,$rid,Request $req)
    {
        if(empty(session('master['.$eid.']')) || $req -> token != session('master['.$eid.']') -> token)
        {
            return view('home.enterprises.error');
        }
        $data = DB::table('recruit as rec') 
                -> leftJoin('regioncate as reg','rec.r_id','=','reg.id')
                -> leftJoin('cate as p','rec.p_id','=','p.id')
                -> leftJoin('cate as c','rec.c_id','=','c.id')
                -> leftJoin('we as w','rec.w_id','=','w.id')
                -> leftJoin('er','rec.er_id','=','er.id')
                -> leftJoin('pms','rec.pms_id','=','pms.id')
                -> leftJoin('enterprises as ent','rec.e_id','=','ent.id')
                -> select('rec.*','reg.name','ent.address','p.name as pN','c.name as cN','w.value as wN','er.value as erN','pms.value as pmsN','ent.compname','ent.corpsize','ent.corptype','ent.firstIndustry')
                -> where('rec.e_id',$eid)
                -> where('rec.id',$rid)
                -> first();
        $ent=DB::table('enterprises as ent') 
                -> leftJoin('corpsize as cor','ent.corpsize','=','cor.id')
                -> leftJoin('corptype as ct','ent.corptype','=','ct.id')
                -> leftJoin('industrycate as ind','ent.firstIndustry','=','ind.id')
                -> select('cor.name as corN','ct.name as ctN','ind.name as indN')
                -> where('cor.id',$data->corpsize)
                -> where('ct.id',$data->corptype)
                -> where('ind.id',$data->firstIndustry)
                -> where('ent.id',$eid)
                -> first();
        $labeld = DB::table('recruit_label as r')
                -> leftJoin('label as l','r.l_id','=','l.id')
                -> where('r.rec_id',$rid)
                -> get();
        // dd($ent);
        $data -> corpsize = $ent -> corN;
        $data -> corptype = $ent -> ctN;
        $data -> firstIndustry = $ent -> indN;
        // dd($data);
        // dd($labeld);
        // dd(session('master['.$eid.']') -> lxfs);
        preg_match('/\">(.*?)<\/a>/', session('master['.$eid.']') -> lxfs,$macth);
        // dd($macth);
        $page = 'http:';
        if(empty($macth[1]))
        {
            $macth[1]=null;
        }
        return view('home.enterprises.view',['data' => $data,'labeld' => $labeld,'id' => $eid,'page' => $macth[1]]);
    }
    public function del($eid,Request $r)
    {
        // dd($r -> except('_token'));
        $delData = $r -> except('_token');
        // dd($delData);
        $resrec = DB::table('recruit') -> delete($delData['rec_id']);
        if($resrec)
        {
           // dd('处理成功');
           $res = DB::table('recruit_label') -> where('rec_id',$delData['rec_id']) -> delete(); 
           if($res)
           {
                return redirect('/home/enterprise/jobs/index/'.$delData['id'].'?token='.$delData['token']);
           }
        }

        // dd($res);
        

    }
    //                          企业  职位  token
    public function jobsmodify($eid,$rid,Request $req)
    {
        if(empty(session('master['.$eid.']')) || $req -> token != session('master['.$eid.']') -> token)
        {
            return view('home.enterprises.error');
        }
        $labelData = DB::table('recruit_label as recl')
                    -> leftJoin('label as l','recl.l_id','=','l.id')
                    -> select('l.*')
                    -> where('recl.rec_id',$rid)
                    -> get();
        if(empty($labelData))
        {
            $labelData=null;
        }else
        {
            foreach($labelData as $k => $v)
            {
                $labelData[$k] = $v -> id;
            }
        }
        // dd($labelData);
        $rData = DB::table('recruit as rec')
                    -> leftJoin('invoice as inv','rec.id','=','inv.rec_id')
                    -> leftJoin('cate as p','rec.p_id','=','p.id')
                    -> leftJoin('cate as c','rec.c_id','=','c.id')
                    -> leftJoin('regioncate as reg','rec.r_id','=','reg.id')
                    -> select('reg.name as cityN','rec.*','inv.id as inv_id','inv.compname','inv.rpostcode','inv.idesc','inv.rname','inv.rphone','inv.city_id','inv.raddress','inv.invoiceheader','p.name as pN','c.name as cN')
                    -> where('rec.id',$rid)
                    -> first();
        if($rData -> invoice_id <> 0)
        {
            $regData = DB::table('regioncate as reg')
                        -> where('id',$rData -> city_id)
                        -> first();
            if($regData -> pid <> 0)
            {
                
                $reData[]=DB::table('regioncate as reg')
                        -> where('id',$regData -> pid)
                        -> first();
                $reData[] = $regData;
            }else
            {
                $reData[] = $regData;
            }
        }else
        {
            $reData[]=null;
        }
            

        // dd($rData);
        // dd(session('master['.$eid.']'));
        // dd($req);
        $label = DB::table('label') ->  get();
        
        $cate = DB::table('cate') -> where('pid','<>','0') ->  get();

        // 只要二级三级分类
        $cateData = [];
        $cateSDate = [];
        foreach ($cate as $key => $value) {
           $num = substr_count($value -> path,',');
           // dd($num);
           if($num == 1)
           {
                // 等于一个是二级分类
                $cateData[]=$value;
           }else if($num == 2)
           {
                $cateSDate[$value -> pid][]=$value;
           }
            // substr_c
        }
        $pidData = DB::table('regioncate') -> where('pid',0) -> where('status',0) -> get();
        $mainData = DB::table('regioncate') -> where('pid',0) -> where('status',1) -> get();
        $er = DB::table('er') -> orderBy('id') ->  get();
        $we = DB::table('we') -> orderBy('id') ->  get();
        $pms = DB::table('pms') ->  get();
        $idData = [];
        foreach($pidData as $k => $v)
        {
            $idData[] = DB::table('regioncate') -> where('pid',$v -> id) -> get();
        }

        $enterpriseData = DB::table('enterprises AS e')
            -> leftJoin('regioncate AS r','e.city','=','r.id')
            -> leftJoin('regioncate AS re','e.province','=','re.id')
            -> leftJoin('industrycate AS i','e.firstIndustry','=','i.id')
            -> leftJoin('corptype AS ct','e.corptype','=','ct.id')
            -> leftJoin('corpsize AS cs','e.corpsize','=','cs.id')
            -> select('e.*','r.name AS cityN','re.name AS provinceN','i.name AS firstIndustryN','ct.name AS ctN','cs.name AS csN')
            -> where('e.id',$eid) -> first();
         if(empty($enterpriseData))
         {
            return view('errors.503');
         }
         if($enterpriseData -> industrylist)
        {
            $tmparr = explode(',',$enterpriseData -> industrylist);
            $enterpriseData -> industrylist = array();
            foreach($tmparr as $k => $v)
            {
                $enterpriseData -> industrylist[DB::table('industrycate') -> where('id',$v) -> select('name') -> first() -> name] = $v;
            }
        }
        if($enterpriseData -> tel)
        {
            $enterpriseData -> tel = explode('-',$enterpriseData -> tel); 
        }
        // array_shift($enterpriseData -> industrylist);
        session(['master['.$eid.']' => $enterpriseData]);
        return view('home.enterprises.jobsmodify',['regData' => $reData,'id' => $eid,'rData' => $rData,'cateData' => $cateData,'cateSData' => $cateSDate,'label' => $label,'labelData' => $labelData,'mainData' => $mainData,'idData' => $idData,'pidData' => $pidData,'we' => $we,'er' => $er,'pms' => $pms]);
    }
    public function jobschang(Request $r)
    {
        // dd($r -> all());
        
            $data=$r -> only('eid','rid','workname','c_id','detail','label','citywork','MinYears','education','employment_type','jobnum','salary','invoice','itype','icorpname','idesc','rname','rphone','padd2','padd3','raddress','rpostcode');
            // dd($data);
            $data1['workname'] = $data['workname'];
            $data1['c_id'] = $data['c_id'][0];
            $data1['p_id'] = $data['c_id'][1];
            if(count($data['c_id']) == 4)
            {
                $data1['c_id'] = $data['c_id'][2];
                $data1['p_id'] = $data['c_id'][3];
            }
            $data1['e_id'] = $data['eid'];
            $data1['r_id']=trim(strrchr($data['citywork'],'_'),'_');
            $data1['description'] = $data['detail'];
            $data1['w_id'] = $data['MinYears'];
            $data1['er_id'] = $data['education'];
            $data1['jobcategory'] = $data['employment_type'];
            $data1['num'] = $data['jobnum'];
            $data1['pms_id'] = $data['salary'];
            // 不需要发票
            $data1['invoice_id']=0;
            $data1['status']=0;
            $data1['date']=time();
            // dd($data1);
            $id = DB::table('recruit') -> where('id',$r -> rid) -> update($data1);
            // 判断是否需要发票
            if($data['invoice']<>0)
            {
                // 需要
                $indata['invoiceheader'] = $data['itype']; // 发票抬头
                $indata['compname'] = $data['icorpname']; // 公司名称
                $indata['idesc'] = $data['idesc']; // 发票内容
                $indata['rname'] = $data['rname']; // 联系人姓名
                $indata['rphone'] = $data['rphone']; // 联系人手机
                
                if(!empty($data['padd3']))
                {
                    $indata['city_id'] = $data['padd3']; // 发往城市的地区id
                }else
                {
                    $indata['city_id'] = $data['padd2']; // 发往城市的地区id
                }
                $indata['raddress'] = $data['raddress']; // 详细地址
                $indata['rpostcode'] = $data['rpostcode']; // 邮编
                // 招聘职位id
                $indata['rec_id'] = $r -> rid;
                // 企业id
                $indata['e_id'] = $data['eid'];
                if($data['invoice'] == 1)
                {
                    $inId = DB::table('invoice') -> insertGetId($indata);
                    if($inId)
                    {
                        $res = DB::table('recruit') -> where('id',$id) -> update(['invoice_id' => $inId]);
                        if(!$res)
                        {
                            return back() -> with('职位发布失败');
                        }
                    }
                }else
                {
                    $inId = DB::table('invoice') -> where('id',$data['invoice']) -> update($indata);
                    if($inId)
                    {
                        $res = DB::table('recruit') -> where('id',$r -> rid) -> update(['invoice_id' => $data['invoice']]);
                        if(!$res)
                        {
                            return back() -> with('职位发布失败');
                        }
                    }
                }
                    
            }
            // 标签表中插入数据
            $inte = [];
            foreach ($data['label'] as $key => $value) {
                $inte[$key]['rec_id'] = $r -> rid;
                $inte[$key]['l_id'] = $value;
            }
            // dd($inte);
            $resdelin = DB::table('recruit_label') -> where('rec_id',$r -> rid) -> delete();
            $resin = DB::table('recruit_label') -> insert($inte);
            if($resin)
            {
                // 发布成功去支付
                // return redirect('/home/enterprise/jobs/index/'.$r -> eid.'?token='.session('master['.$r -> eid.']') -> token) -> with(['info' => '职位发布成功']);
                if(session('master['.$r -> eid.']') -> authentication == '5')
                {
                    return redirect('/home/enterprise/jobs/pay/'.$r -> eid.'/'.$r -> rid.'?token='.session('master['.$r -> eid.']') -> token);
                }else
                {
                   return redirect('/home/enterprise/jobs/index/'.$r -> eid.'?token='.session('master['.$r -> eid.']') -> token) -> with(['info' => '抱歉你未通过审核,所发布的职位不能支付']);
                }
            }else
            {
                dd('职位失败');
            }
               
        
        
        // $this -> changes($r -> id,$r);
    }
    public function jobspay($eid,$rid,Request $req)
    {
        // dd($r);
        if(empty(session('master['.$eid.']')) || $req -> token != session('master['.$eid.']') -> token)
        {
            return view('home.enterprises.error');
        }
        if(session('master['.$req -> eid.']') -> authentication <> '5')
        {
           return redirect('/home/enterprise/jobs/index/'.$req -> eid.'?token='.session('master['.$req -> eid.']') -> token) -> with(['info' => '抱歉你未通过审核,所发布的职位不能支付']);
        }
        $rData = DB::table('recruit as rec')
                    -> leftJoin('invoice as inv','rec.id','=','inv.rec_id')
                    -> leftJoin('cate as p','rec.p_id','=','p.id')
                    -> leftJoin('cate as c','rec.c_id','=','c.id')
                    -> leftJoin('er','rec.er_id','=','er.id')
                    -> leftJoin('we','rec.w_id','=','we.id')
                    -> leftJoin('pms','rec.pms_id','=','pms.id')
                    -> leftJoin('regioncate as reg','rec.r_id','=','reg.id')
                    -> leftJoin('enterprises as ent','rec.e_id','=','ent.id')
                    -> select('pms.value as pmsN','er.value as erN','we.value as weN','reg.name as cityN','ent.compname','rec.*','inv.id as inv_id','inv.compname','inv.rpostcode','inv.idesc','inv.rname','inv.rphone','inv.city_id','inv.raddress','inv.invoiceheader','p.name as pN','c.name as cN')
                    -> where('rec.id',$rid)
                    -> where('rec.sale',0)
                    -> first();
        // dd($rData);
        if(empty($rData))
        {
        return redirect('/home/enterprise/jobs/index/'.$eid.'?token='.session('master['.$eid.']') -> token) -> with(['info' => '操作无效']);

        }
        // if()
        if($rData -> invoice_id <> 0)
        {
            $regData = DB::table('regioncate as reg')
                        -> where('id',$rData -> city_id)
                        -> first();
            if($regData -> pid <> 0)
            {
                
                $reData[]=DB::table('regioncate as reg')
                        -> where('id',$regData -> pid)
                        -> first();
                $reData[] = $regData;
            }else
            {
                $reData[] = $regData;
            }
        }else
        {
            $reData[]=null;
        }
        // dd($rData);
        if($rData -> sales <> 0)
        {
          DB::table('recruit') -> where('id',$rid) -> update(['sale' => 1]);
          DB::table('recruit') -> where('e_id',$eid) -> update(['sales' => $rData -> sales - 1]);
          return redirect('/home/enterprise/jobs/index/'.$eid.'?token='.session('master['.$eid.']') -> token) -> with(['info' => '支付成功,等待职位审核']);  
        }
        return view('home.enterprises.pay',['id' => $eid,'rData' => $rData]);
    }
    public function paystatus(Request $r)
    {
        // dd($r -> all());
        $data = $r -> all();
        DB::table('recruit') -> where('id',$data['rid']) -> update(['sale' => 1]);
        return redirect('/home/enterprise/jobs/index/'.$r -> eid.'?token='.session('master['.$r -> eid.']') -> token) -> with(['info' => '支付成功,等待职位审核']);
        
    }







    // 简历
    public function resumesindex($eid , Request $req)
    {
       // var_dump(session('masterarr'));
       // session(['masterarr' => array(0=>10102)]);
       // session(['masterarr' => array(1=>10101)]);
       // session(['masterarr' => 10102]);
       
        // var_dump(session('masterarr'));
       

        // dd(session('masterarr'));
        if(empty(session('master['.$eid.']')) || $req -> token != session('master['.$eid.']') -> token)
        {
            return view('home.enterprises.error');
        }
        // 待处理的简历
        $resumesData[0] = DB::table('resume_recruit as resrec')
                             -> leftJoin('recruit as rec','resrec.rec_id','=','rec.id')
                             -> leftJoin('resume as res','resrec.res_id','=','res.id')
                             -> leftJoin('interviewee as int','resrec.int_id','=','int.id')
                             -> select('int.*','resrec.*','rec.*','res.*','resrec.id as resrec_id','resrec.int_id','resrec.res_id')
                             -> where('resrec.ent_id',$eid)
                             -> where('resrec.status',0)
                             -> paginate(10);

        foreach($resumesData[0] as $k => $v)
        {
           // dd($v);
           $resumesData[0][$k] -> CurrentCareerStatus = DB::table('resume_status') -> select('value') -> where('id',$v -> CurrentCareerStatus) -> first() -> value;
           $resumesData[0][$k] -> DesiredSalaryScope = DB::table('pms') -> select('value') -> where('id',$v -> DesiredSalaryScope) -> first() -> value;
           $resumesData[0][$k]  -> DesiredIndustry = DB::table('industrycate') -> select('name') -> where('id',$v -> DesiredIndustry) -> first() -> name;
           $resumesData[0][$k]  -> desired_Jobtype_id = DB::table('cate') -> select('name') -> where('id',$v -> desired_Jobtype_id) -> first() -> name;
           $resumesData[0][$k]  -> p_id = DB::table('cate') -> select('name') -> where('id',$v -> p_id) -> first() -> name;
           $resumesData[0][$k]  -> c_id = DB::table('cate') -> select('name') -> where('id',$v -> c_id) -> first() -> name;
           $resumesData[0][$k]  -> DesiredCity = DB::table('regioncate') -> select('name') -> where('id',$v -> DesiredCity) -> first() -> name;
           $resumesData[0][$k]  -> EducationLevel = DB::table('er') -> select('value') -> where('id',$v -> EducationLevel) -> first() -> value;
           $resumesData[0][$k]  -> r_id = DB::table('regioncate') -> select('name') -> where('id',$v -> r_id) -> first() -> name;
           $here = DB::table('regioncate') -> where('name',$v -> here) -> first();
           // dd($here);
           if($here -> pid == 0)
           {
             $resumesData[0][$k]  -> here = array($here -> name);
           }else
           {
             $pidname = DB::table('regioncate') -> select('name') -> where('id',$here -> pid) -> first() -> name;
             $resumesData[0][$k]  -> here = array($pidname,$here -> name);
           }
           
           $city = DB::table('regioncate') -> where('name',$v -> city) -> first();
           if($city -> pid == 0)
           {
             $resumesData[0][$k]  -> city = array($city -> name);
           }else
           {
             $pidname = DB::table('regioncate') -> select('name') -> where('id',$city -> pid) -> first() -> name;
             $resumesData[0][$k]  -> city = array($pidname,$city -> name);
           }
           $resumesData[0][$k]  -> DesiredEmploymentType = explode(',',$resumesData[0][$k]  -> DesiredEmploymentType);
        }                     
                             
        // dd($resumesData[0]);
        //  待沟通

        $resumesData[1] = DB::table('resume_recruit as resrec')
                             -> leftJoin('recruit as rec','resrec.rec_id','=','rec.id')
                             -> leftJoin('resume as res','resrec.res_id','=','res.id')
                             -> leftJoin('interviewee as int','resrec.int_id','=','int.id')
                             -> select('int.*','resrec.*','rec.*','res.*','resrec.id as resrec_id','resrec.int_id','resrec.res_id')
                             -> where('resrec.ent_id',$eid)
                             -> where('resrec.status',1)
                             -> paginate(10);

        foreach($resumesData[1] as $k => $v)
        {
           // dd($v);
           $resumesData[1][$k] -> CurrentCareerStatus = DB::table('resume_status') -> select('value') -> where('id',$v -> CurrentCareerStatus) -> first() -> value;
           $resumesData[1][$k] -> DesiredSalaryScope = DB::table('pms') -> select('value') -> where('id',$v -> DesiredSalaryScope) -> first() -> value;
           $resumesData[1][$k]  -> DesiredIndustry = DB::table('industrycate') -> select('name') -> where('id',$v -> DesiredIndustry) -> first() -> name;
           $resumesData[1][$k]  -> desired_Jobtype_id = DB::table('cate') -> select('name') -> where('id',$v -> desired_Jobtype_id) -> first() -> name;
           $resumesData[1][$k]  -> p_id = DB::table('cate') -> select('name') -> where('id',$v -> p_id) -> first() -> name;
           $resumesData[1][$k]  -> c_id = DB::table('cate') -> select('name') -> where('id',$v -> c_id) -> first() -> name;
           $resumesData[1][$k]  -> DesiredCity = DB::table('regioncate') -> select('name') -> where('id',$v -> DesiredCity) -> first() -> name;
           $resumesData[1][$k]  -> EducationLevel = DB::table('er') -> select('value') -> where('id',$v -> EducationLevel) -> first() -> value;
           $resumesData[1][$k]  -> r_id = DB::table('regioncate') -> select('name') -> where('id',$v -> r_id) -> first() -> name;
           $here = DB::table('regioncate') -> where('name',$v -> here) -> first();
           // dd($here);
           if($here -> pid == 0)
           {
             $resumesData[1][$k]  -> here = array($here -> name);
           }else
           {
             $pidname = DB::table('regioncate') -> select('name') -> where('id',$here -> pid) -> first() -> name;
             $resumesData[1][$k]  -> here = array($pidname,$here -> name);
           }
           
           $city = DB::table('regioncate') -> where('name',$v -> city) -> first();
           if($city -> pid == 0)
           {
             $resumesData[1][$k]  -> city = array($city -> name);
           }else
           {
             $pidname = DB::table('regioncate') -> select('name') -> where('id',$city -> pid) -> first() -> name;
             $resumesData[1][$k]  -> city = array($pidname,$city -> name);
           }
           $resumesData[1][$k]  -> DesiredEmploymentType = explode(',',$resumesData[1][$k]  -> DesiredEmploymentType);
        } 
        //  已发面试邀请
         $resumesData[2] = DB::table('resume_recruit as resrec')
                             -> leftJoin('recruit as rec','resrec.rec_id','=','rec.id')
                             -> leftJoin('resume as res','resrec.res_id','=','res.id')
                             -> leftJoin('interviewee as int','resrec.int_id','=','int.id')
                             -> select('int.*','resrec.*','rec.*','res.*','resrec.id as resrec_id','resrec.int_id','resrec.res_id')
                             -> where('resrec.ent_id',$eid)
                             -> where('resrec.status',2)
                             -> paginate(10);
        // dd($resumesData[2]);
        foreach($resumesData[2] as $k => $v)
        {
           // dd($v);
           $resumesData[2][$k] -> CurrentCareerStatus = DB::table('resume_status') -> select('value') -> where('id',$v -> CurrentCareerStatus) -> first() -> value;
           $resumesData[2][$k] -> DesiredSalaryScope = DB::table('pms') -> select('value') -> where('id',$v -> DesiredSalaryScope) -> first() -> value;
           $resumesData[2][$k]  -> DesiredIndustry = DB::table('industrycate') -> select('name') -> where('id',$v -> DesiredIndustry) -> first() -> name;
           $resumesData[2][$k]  -> desired_Jobtype_id = DB::table('cate') -> select('name') -> where('id',$v -> desired_Jobtype_id) -> first() -> name;
           $resumesData[2][$k]  -> p_id = DB::table('cate') -> select('name') -> where('id',$v -> p_id) -> first() -> name;
           $resumesData[2][$k]  -> c_id = DB::table('cate') -> select('name') -> where('id',$v -> c_id) -> first() -> name;
           $resumesData[2][$k]  -> DesiredCity = DB::table('regioncate') -> select('name') -> where('id',$v -> DesiredCity) -> first() -> name;
           $resumesData[2][$k]  -> EducationLevel = DB::table('er') -> select('value') -> where('id',$v -> EducationLevel) -> first() -> value;
           $resumesData[2][$k]  -> r_id = DB::table('regioncate') -> select('name') -> where('id',$v -> r_id) -> first() -> name;
           $here = DB::table('regioncate') -> where('name',$v -> here) -> first();
           // dd($here);
           if($here -> pid == 0)
           {
             $resumesData[2][$k]  -> here = array($here -> name);
           }else
           {
             $pidname = DB::table('regioncate') -> select('name') -> where('id',$here -> pid) -> first() -> name;
             $resumesData[2][$k]  -> here = array($pidname,$here -> name);
           }
           
           $city = DB::table('regioncate') -> where('name',$v -> city) -> first();
           if($city -> pid == 0)
           {
             $resumesData[2][$k]  -> city = array($city -> name);
           }else
           {
             $pidname = DB::table('regioncate') -> select('name') -> where('id',$city -> pid) -> first() -> name;
             $resumesData[2][$k]  -> city = array($pidname,$city -> name);
           }
           $resumesData[2][$k]  -> DesiredEmploymentType = explode(',',$resumesData[2][$k]  -> DesiredEmploymentType);
        } 
        //  不合适
        // dd($resumesData[2]);
        $resumesData[3] = DB::table('resume_recruit as resrec')
                             -> leftJoin('recruit as rec','resrec.rec_id','=','rec.id')
                             -> leftJoin('resume as res','resrec.res_id','=','res.id')
                             -> leftJoin('interviewee as int','resrec.int_id','=','int.id')
                             -> select('int.*','resrec.*','rec.*','res.*','resrec.id as resrec_id','resrec.int_id','resrec.res_id')
                             -> where('resrec.ent_id',$eid)
                             -> where('resrec.status',4)
                             -> paginate(10);

        foreach($resumesData[3] as $k => $v)
        {
           // dd($v);
           $resumesData[3][$k] -> CurrentCareerStatus = DB::table('resume_status') -> select('value') -> where('id',$v -> CurrentCareerStatus) -> first() -> value;
           $resumesData[3][$k] -> DesiredSalaryScope = DB::table('pms') -> select('value') -> where('id',$v -> DesiredSalaryScope) -> first() -> value;
           $resumesData[3][$k]  -> DesiredIndustry = DB::table('industrycate') -> select('name') -> where('id',$v -> DesiredIndustry) -> first() -> name;
           $resumesData[3][$k]  -> desired_Jobtype_id = DB::table('cate') -> select('name') -> where('id',$v -> desired_Jobtype_id) -> first() -> name;
           $resumesData[3][$k]  -> p_id = DB::table('cate') -> select('name') -> where('id',$v -> p_id) -> first() -> name;
           $resumesData[3][$k]  -> c_id = DB::table('cate') -> select('name') -> where('id',$v -> c_id) -> first() -> name;
           $resumesData[3][$k]  -> DesiredCity = DB::table('regioncate') -> select('name') -> where('id',$v -> DesiredCity) -> first() -> name;
           $resumesData[3][$k]  -> EducationLevel = DB::table('er') -> select('value') -> where('id',$v -> EducationLevel) -> first() -> value;
           $resumesData[3][$k]  -> r_id = DB::table('regioncate') -> select('name') -> where('id',$v -> r_id) -> first() -> name;
           $here = DB::table('regioncate') -> where('name',$v -> here) -> first();
           // dd($here);
           if($here -> pid == 0)
           {
             $resumesData[3][$k]  -> here = array($here -> name);
           }else
           {
             $pidname = DB::table('regioncate') -> select('name') -> where('id',$here -> pid) -> first() -> name;
             $resumesData[3][$k]  -> here = array($pidname,$here -> name);
           }
           
           $city = DB::table('regioncate') -> where('name',$v -> city) -> first();
           if($city -> pid == 0)
           {
             $resumesData[3][$k]  -> city = array($city -> name);
           }else
           {
             $pidname = DB::table('regioncate') -> select('name') -> where('id',$city -> pid) -> first() -> name;
             $resumesData[3][$k]  -> city = array($pidname,$city -> name);
           }
           $resumesData[3][$k]  -> DesiredEmploymentType = explode(',',$resumesData[3][$k]  -> DesiredEmploymentType);
        } 
        // dd($resumesData);
        // dd($eid);
        return view('home.enterprises.resumes',['id' => $eid,'data' => $resumesData]);
    }
    public function status($eid,$resid,$mode,Request $r)
    {
        if(empty(session('master['.$eid.']')) || $r -> token != session('master['.$eid.']') -> token)
        {
            return view('home.enterprises.error');
        }
        // dd($mode);
        if($mode == 4)
        {
            $res = DB::table('resume_recruit') -> where('id',$resid) -> update(['status' => 4]);
        }else if($mode == 2)
        {
            $emD = DB::table('resume_recruit') -> where('id',$resid) -> first();

            $intD = DB::table('interviewee') -> where('id',$emD -> int_id) -> first();
            $recD = DB::table('recruit') -> where('id',$emD -> rec_id) -> first();
            $entD = DB::table('enterprises') -> where('id',$emD -> ent_id) -> first();
            // $email = $intD -> email;
            $city = DB::table('regioncate') -> where('id',$entD -> city) -> first();
            if($city -> pid == 0)
            {
              $entD  -> city = array($city -> name);
            }else
            {
              $pidname = DB::table('regioncate') -> select('name') -> where('id',$city -> pid) -> first() -> name;
              $entD  -> city = array($pidname,$city -> name);
            }

            // dd($entD);
            $mail = $intD -> email;
            $mail = 'email.'.trim(strchr($mail, '@'), '@');

            $s = Mail::send('emails.mail', ['intD' => $intD,'rec' => $recD,'ent' => $entD], function ($message)
            use ($intD) { // use ($user) 语法 php5新出的语法 在匿名函数内使用匿名函数外的局部变量
                                          // 发送到模板文件中的一些变量
                                          // 比如用户名 ***你好,你申请进行密码找回
                // $m->from('hello@app.com', 'Your Application');
                // 邮件的接收方                       // 发送的邮件主题
                $message -> to($intD -> email) -> subject('招聘面试邮件');
            });
            // dd('ffff');
            if($s)
            {
                with(['mail' => '邮件发送成功']);
                // return redirect('/admin/reset/info') -> with(['info' => '邮件发送成功','url' => $mail]);
            }
            $res = DB::table('resume_recruit') -> where('id',$resid) -> update(['status' => 2]);
        }else if($mode == 1)
        {
            $res = DB::table('resume_recruit') -> where('id',$resid) -> update(['status' => 1]);
        }else if($mode == 6)
        {
            $res = DB::table('resume_recruit') -> where('id',$resid) -> update(['status' => 6]);
            if($res)
            {
                $ridD = DB::table('resume_recruit') -> where('id',$resid) -> first();
                $rid = $ridD -> rec_id;
                $ridC = DB::table('recruit') -> where('id',$rid) -> first();
                $ridN = $ridC -> num;
                if($ridN > 0)
                {
                    $ridN--;
                    $res = DB::table('recruit') -> where('id',$rid) -> update(['num'=>$ridN]);
                }else
                {
                    $res = 0;
                }
                
            }else
            {
                return redirect('/home/enterprise/resumes/'.$r -> eid.'?token='.session('master['.$r -> eid.']') -> token) -> with(['info' => '处理失败']);
            }
            
        }
        if($res)
        {
            return redirect('/home/enterprise/resumes/'.$r -> eid.'?token='.session('master['.$r -> eid.']') -> token) -> with(['info' => '处理成功']);
        }
        return redirect('/home/enterprise/resumes/'.$r -> eid.'?token='.session('master['.$r -> eid.']') -> token) -> with(['info' => '处理失败']);
    }
    // 简历搜索
    // public function search($eid , Request $req)
    // {
    //     if(empty(session('master['.$eid.']')) || $req -> token != session('master['.$eid.']') -> token)
    //     {
    //         return view('home.enterprises.error');
    //     }
        // dd(session('master['.$eid.']'));
        // dd($eid);
    //     return view('home.enterprises.search',['id' => $eid]);
        
    // }
    // public function searchr(Request $r)
    // {
    //     if(empty(session('master['.$r -> eid.']')) || $r -> token != session('master['.$r -> eid.']') -> token)
    //     {
    //         return view('home.enterprises.error');
    //     }
    //     $searData = $r -> all();
    //     $searchData = explode(' ',$searData['SF_1_1_1']);
    //     $sD = [];
    //     foreach ($searchData as $key => $value) {
    //        if(!empty($value))
    //        {
    //          $sD[]='%'.$value.'%';
    //        }
    //     }

    //     DB::table('resumes') -> 
    //     // dd($sD);
    //     // dd($r->all());
    // }

    // 退出
    public function loginout($eid)
    {
        // 清除session中的master的值
        session() -> forget('master['.$eid.']');
        // session() -> flush(); 清空session
        // dd(session() -> all());
        return redirect('/home/enterprise/reglogin') -> with(['info' => '您以安全退出!!']);
    }
}
